\section{Appendices.}
\appendix
\section{The problem of altitude estimation.}

The Earths atmosphere is a extremely thin layer of gas that extends from it's
surface up to what is defined as the limit with space, 100km above. The force
of gravity maintains this layer of gas attached to the surface of our
planet. Within this layer several very complex chemical, thermodynamical and
fluidodynamical phenomena take place. As the atmosphere is not uniform, it's
properties change constantly. We call these changes with names like weather
and climate, and Meteorology is the name of the Science that studies and tries
to build models to predict these changes.

The properties of the air change from the surface of the Earth upwards. The
Sun rises the Earths surface temperature, heating the air near it. The hot air
moves and rises by convection and difussion. Because of this, the air near the
surface is hotter, and it's temperature decreases as we move higher.

The speed of sound depends on the air temperature, and also slows down with
the altitude.

The air pressure is in direct proportion to the weight of the air column above
a certain region. As we go up in the atmosphere, the amount of air above is
less and, accordingly, the pressure decreases with height.

Air density depends on temperature and pressure, and also decreases with
height.

To make use of all these relations in diverse scientific diciplines and
techniques related to the atmosphere, it is necessary to build mathematical
models that allow us a reasonable prediction of the variation of these
properties.

These models are built using measurements of different atmospheric
variables. These measurements are obteined from different sources, like
meteorological stations, sounding balloons, etc. These measurements are
properly processed, and then theoretical models are fitted to them. In this
way one obtains equations which are useful for the prediction of atmosphere
behaviour.

Nowadays several models exist that contemplate different situations:

\begin{itemize}
\item Average or standard day.
\item Hot day.
\item Cold day.
\item Tropical day
\end{itemize}

These models are updated every few years, so as to include the latest atmospheric measurements.

The models we are going to describe, developed in the sixties, asume that the
preassure, expressed in kiloPascal (kPa), and the temperature, expressed in
degrees Celcius (C), only change with the altitude h, expressed in meters,
according to the following equations, defined for three different zones of the
atmosphere:

\begin{enumerate}
\item High stratosphere: above 25000m.
  \begin{gather*}
    \mathbf{T} = -131.21 + 0.00299 \times \mathbf{h} \\
    \mathbf{p} = 2.488 \times {\left( \frac{\mathbf{T} + 273.1}{216.6} \right)}^{-11.388}
  \end{gather*}

\item Low stratosphere: between 11000 and 25000m.
  \begin{gather*}
    \mathbf{T}=-56.46 \\
    \mathbf{p}=22.65 \times e^{\left(1.73 - 0.000157 \times \mathbf{h}\right)}
  \end{gather*}

\item Troposphere: below 11000m.
  \begin{gather}
    \mathbf{T}=15.04 - 0.00649 \times \mathbf{h} \label{temperatura} \\
    \mathbf{p} = 101.29 \times {\left( \frac{\mathbf{T} + 273.1}{288.08}
    \right)}^{5.256} \label{presion}
  \end{gather}
\end{enumerate}


The working zone for our flights lies within the \textbf{TROPOSPHERE} (below 11000m).

According to equations \ref{temperatura} and \ref{presion}, we can see that
the relationship between pressure $\mathbf{p}$ and height $\mathbf{h}$ is exponential.

The next graph shows this relationship.

\begin{center}
\scalebox{0.40}[0.25]{\includegraphics{modelo_atmosfera.png}}
\end{center}

Our problem consists in the estimation of height $\mathbf{h}$ from pressure
$\mathbf{p}$ data collected from the sensor.

From equations \ref{temperatura} and \ref{presion}, we can write an expression
that enables us to calculate height $\mathbf{h}$ :

\begin{gather}
\mathbf{h} = \left( \frac{1}{0.00649}\right) \times 288.08 \times
\left(1-\sqrt[5.256]{\frac{\mathbf{p}}{101.29}}\right) \label{altura}
\end{gather}

This happens in the analog world, which is continuous and infinite. But our
altimeter works in a digital world, which is discrete and finite.

The analog/digital converter included in the microchip has a resolution of
only 10 bits. This means that it is able to represent its work range (0 to 5
V) in $2^{10}$ =1024 levels. This determines the resolution of $0.004888$\,V/count.

The absolute pressure sensor MPX5100, has a work range between 0.2V and 4.7V,
so it can be connected to the analog/digital converter directly. Noise
elimination is implemented in software, using a recursive digital filter.

The pressure work range is between 115 kPa and 15 kPa, which gives us a
resolution of 22.22 kPa/V.

The relationship between pressure and the voltage delivered by the sensor is
linear, according to the following transfer function:

\begin{center}
\scalebox{0.6}{\includegraphics{funcion_sensor.png}}
\end{center}


The microcontroler used in the altimeter can only do very simple arithmetic
operations like addition, substraction, and division/multiplication by
2. These operations can only be applied to bytes (8 bits). With one byte its
only possible to represent 256 values (from 0 to 255). These are the
calculation tools available to solve this problem. Further restrinctions come
from the fact tha very little memory for storing data is available (64 bytes
of RAM and 128 byes of EEPROM), and only 1024 words for programs.

Two problems must be solved, subject to these restrinctions: 

\begin{enumerate}
\item  An adequate number representation for the problem in hand.
\item An afficient algorithm to estimate altitude according to equation \ref{altura}.
\end{enumerate}

The number representation problem is solved using a fixed point representation
using multiple bytes. It's our responsability that operations using this
numerical representation is done properly.

To solve the second problem it was decided that the exponential curve would be
represented by 16 linear segments. A table is stored in memory, with data for
each segment (slope and y-intercept). According to the pressure meassured, the
proper segment is chosen and the height is caclulated using that segments
data.

The advantage of this kind of calculation is that a linear function y=ax+b is
easily calculated using a multiplication routine adapted to the selected data
model.

\newpage
\section{Communication Protocol.}

\subsection{``PROTOCOL'' definition.}

The term ``protocol'' for data communication procedures was used for the first
time in a memo written in april 1967 by A. Scantlebury and K. A. Bartlett,
at the National Physics Laboratory, England. The memo was entitled ``A protocol
for use in the NPL data communications network''.

A protocol is a set of rules governing the exchange of information in a
distributed system. In fact, the complete definition of protocol goes beyond
the definition of a language:

\begin{itemize}
\item Defines a precise way of validating messages, like for example the dots
and dashes of Morse code (a syntax).
\item Defines the procedure for information interchange (the grammar).
\item Defines the vocabulary of valid messages that can be interchanged, along
with its meaning (semantics).
\end{itemize}

\subsection{Communications Protocol Specification.}

When ptocolos are compared or chosen, or when a basic protocol is designed,
enough detail should be used to characterize it, but no so much as to loose
the global image. Therefore we suggest the following items for the
communication protocols especification:

\begin{itemize}
\item What configuration will be assigned to the protocol, and what kind of
communication will be controlled?
\item Which data blocks will be transferred (and of what length)?
\item What communication functions will be implemented?
\item Which will be the words used in the protocol, and what format will
  be used?
\item Which will be the values of the protocol parameters used ?
\item Which will be the sequence of words of the protocol, and which will be the
phases ot the communication?
\item Which should be the behaviour to each word of the protocol (and which are
the corresponding state diagrams)?
\end{itemize}

The first 5 items need to be specified for any protocol, while the last two
are alternatives: only one has to be specified.

\subsection{Human Machine Interfase (HMI) Protocol.}

\subsubsection{Specification and general considerations.}

\begin{itemize}
\item The protocol is a one-way, acustic and visual protocol, in which the altimeter
does the monologue.
\item The signals are single tone/flash.
\item The length of the frames (sequence) is variable, depending on the values to be
transmitted.
\item A Pause between Frames (PT) is defined as a period of 3 seconds without any
tone/flash.
\item \texttt{A PAUSE BETWEEN VALUES OF A SAME FRAME \textbf{(PV)}}, is a period of 2 seconds without
any tone/flash.
\item \texttt{A PAUSE BETWEEN SIGNALS OF A SAME VALUE \textbf{(PS)}}, is a
period of 500 milliseconds without any tone/flash.
\item \texttt{A SHORT SIGNAL \textbf{(SC} or ``\textbf{\Huge{.}}'')}, is a
tone/flash of 500 milliseconds of duration.
\item \texttt{A LONGH SIGNAL \textbf{(SL} or ``\textbf{\Huge{---}}'')} is a
tone/flash of 2 seconds of duration.
\end{itemize}

\subsubsection{Structure of a Frame (Sequence).}

\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|} \hline
 \texttt{VALUE 0} & \texttt{PV} & \texttt{VALUE 2} & \texttt{PV} &
 $\longrightarrow$ & \texttt{VALUE n} & \texttt{PT} \\ \hline
\end{tabular}
\end{center}

The length will be determined by the number of values to be transmitted.

\subsubsection{Structure of the Values.}

\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|} \hline
 \texttt{SIGNAL 0} & \texttt{PS} & \texttt{SIGNAL 2} & \texttt{PS} &
 $\longrightarrow$ & \texttt{SIGNAL n} & \texttt{PS} \\ \hline
\end{tabular}
\end{center}

The length will be determined by the value to be transmitted.

\subsubsection{Representation of logical values.}
\begin{description}

\item [OK] 

\begin{tabular}{|c|c|c|c|} \hline
\textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} \\ \hline
\end{tabular}

\item [NO OK] 

\begin{tabular}{|c|} \hline
\texttt{\textbf{\Huge{---}}} \\ \hline
\end{tabular}

\end{description}

\subsubsection{Representation of numerical values..}
\begin{tabular}{|ccccccccccccccccccc|} \hline

0 $\rightarrow$ & \texttt{\textbf{\Huge{---}}} & & & & & & & & & & & & & & & & & \\   

1 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & & & & & & & & & & & & & & & & \\

2 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & & & & & & & & & & & & & & \\

3 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & & & & & & & & & & & & \\

4 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & & & & & & & & & & \\

5 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & & & & & & & & \\

6 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & & & & & & \\

7 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} &  & & & \\

8 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} &  & \\
 
9 $\rightarrow$ & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} &
\textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} & \textbf{\Huge{.}} & \texttt{PS} \\ \hline

\end{tabular}

\subsection{ALTIMETER-PC Communication Protocol.}

\subsubsection{Specifications and general considerations.}

\begin{itemize}
\item The protocol follows the Master-Slave model, in which the PC takes the
place of the ``Master'', and the altimeter is the ``Slave''.
\item This implies that the PC has the initiative. The altimeter only answers to
commands sent by the PC.
\item There will be 3 communication frames (sequences) of fixed length, one
for commands, one for addresses and finaly a thid one for data.
\end{itemize}

\subsubsection{Structure of the communication.}

The communication between PC an altimeter will follow this cycle:

\begin{enumerate}
\item The PC sends a command along with the data and address, or only the
   address, depending on the command
\item The altimeter answers to this command with other commands or data according
   to what is requiered.
\item These steps will be repeated until the PC sends the "Quit Configuration"
   command.
\end{enumerate}

\subsubsection{Structure of the command frame (sequence).}

\begin{center}
\begin{tabular}{|c|} \hline
COMMAND \\
(1 BYTE) \\ \hline
\end{tabular}
\end{center}

It has a fixed length of one byte, and the range of general commands is 00H to 7FH.

\subsubsection{Structure of the data frame (sequence).}

\begin{center}
\begin{tabular}{|c|} \hline
DATA \\
(1 BYTE) \\ \hline
\end{tabular}
\end{center}

It's length is fixed: one byte.

\subsubsection{Structure of address frame.}

\begin{center}
\begin{tabular}{|c|} \hline
ADDRESS \\
(1 BYTE) \\ \hline
\end{tabular}
\end{center}

It has a fixed length of one byte.

\subsubsection{Protocol Commands.}

These are the commands sent by the PC ("Master"):

\begin{description}
\item [00H] : Quit configuration.
\item [02H] : Read configuration data from EEPROM.
\item [04H] : Record configuration data to EEPROM.
\end{description}

These are the commands sent by the altimeter ("Slave"):

\begin{description}
\item [01H] : Illegal command.
\item [03H] : Command completed (executed).
\end{description}

\newpage
\section{Flow Diagram of Main Program.}
\begin{center}
\input{prin01.pic}
\end{center}

\newpage
\begin{center}
\input{prin02.pic}
\end{center}

\newpage
\vspace{2cm}
\begin{center}
\input{prin03.pic}
\end{center}

\newpage
\begin{center}
\input{prin04.pic}
\end{center}

\newpage
\begin{center}
\input{prin05.pic}
\end{center}

\newpage
\begin{center}
\input{prin06.pic}
\end{center}

\newpage
\begin{center}
\input{prin07.pic}
\end{center}

\newpage
\begin{center}
\input{prin08.pic}
\end{center}

\newpage
\subsection*{Note \#1:}

The initialization process of a microcontroller implies the configuration of
all it's functionality, using sequences of instructions many times executed in
a specific order, which is defined in the manufacturer's manual.

The first thing to do is to disable interrupt services, to avoid erratic
behaviour of the altimeter. It's a very important security measure. We must
take into account that at that moment, the rocket can be ready on the lauching
pad, with all it's pirotechnique charges armed.

This procedure should be implemented using the sequence of instructions
specified by the manufacturer.

There are other microcontroller initialization tasks, like:

\begin{itemize}
\item Configuration of the function associated to each microcontroller pin.
\item Configuration of the A/D converter:
   \begin{itemize}
   \item Reference voltage selection.
   \item Selection of digital data format.
   \item Conversion time (4\,$\mu$s in our case).
   \item Selection of the channel to be used.
   \end{itemize}
\item Interrupt configuration. In our case the only interruption which will be
considered is the one provoqued by the TIMER0 overflow, that happens every
4.096 ms.
\item Calibration of the the microcontroller internal oscillator, with the
default factory value.
\end{itemize}

\subsection*{Note \#2:}

The detection of apogee is based on a statistical bayesian estimator (based on
some previously known information). We suppoose that the pressure recorded is
maximum when the altimeter is started. As the rocket flies upwards, the
preassure decreases, until a minimum is reached. At that moment the minimum is
corroborated using a confidence corridor.

\subsection*{Note \#3:}

At this point the pressure difference is calculated, using the pressure at
ramp as a reference, to eject the main recovery sistem in case the rocket has
a dual deployment system (drogue and main):

\begin{center}
Main pressure $=$ Pressure on ramp - $\Delta$ Main pressure
\end{center}

\subsection*{Note \#4:}

For the calculation of the maximum altitude reached, we use the formula:

\begin{center}
Max altitude $=$ Apogee absolute altitude - Ramp absolute altitude
\end{center}

To better understand the problems associated with the calculation of altitude
and its implementation on firmware, see the corresponding appendix.

\newpage
\section{Communications routine with PC.}
\input{comm01.pic}

The parameters tha can be modified in the altimeter are the following:

\begin{itemize}
\item Mach delay.
\item Main recovery system altitude.
\item The apogee value. It can be reset after reading.
\item The operation mode.
\end{itemize}

The EEPROMs reading/writing routines receive the address on wich the operation
will be performed.  We believe that the GUI used in the application that runs
on the PC is simple enough and requires no further explanation.

\newpage
\section{Flow Diagram of the Interrupt Service Routines \textbf{ISR}.}

\begin{center}
\input{isr.pic}
\end{center}

\newpage
\section{Annotations.}

\newpage
\vspace*{20cm}
\begin{center}
\shabox{\small{This document was prepared using \LaTeX{} ( Leslie Lamport et al.)}
For more information please go to  \href{http://www.tug.org}{\textbf{TUG}} -
\textbf{\TeX{} Users Group}}
\end{center}
